﻿using DFGK.IService.LY;
using DFGK.Service.LY;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models.LyModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.PC.装备智能采购辅助软件
{
    /// <summary>
    /// 供应商对账退货报损报价
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class SupplierTransactionController : BaseController
    {
        private readonly ISupplierTransactionService _transactionService;
        public SupplierTransactionController(IWebHostEnvironment environment, ISupplierTransactionService transactionService) : base(environment)
        {
            _transactionService = transactionService;
        }
        /// <summary>
        /// 供应商对账退货报损报价添加或修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddOrModefiySupplierTransaction(SupplierTransaction model)
        {
            if (model.Id > 0)
            {
                model.ModifyDate = DateTime.Now;
                model.ModifyUserId = base.BaseGetNowUserId();
                model.ModifyUserName = base.BaseGetNowUserName();
                bool result = _transactionService.AddOrModefiySupplierTransaction(model, out string message);
                if (result)
                {
                    return Ok(BaseResult.Result(true, "修改成功！", "200"));
                }
                return Ok(BaseResult.Result(false, "修改失败！" + message, "500"));
            }
            else
            {
                model.CreateUserName = base.BaseGetNowUserName();
                model.CreateUserId = base.BaseGetNowUserId();
                model.CreateDate = DateTime.Now;
                bool result = _transactionService.AddOrModefiySupplierTransaction(model, out string message);
                if (result)
                {
                    return Ok(BaseResult.Result(true, "添加成功！", "200"));
                }
                return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));


            }
        }
        /// <summary>
        /// 供应商对账退货报损报价删除
        /// </summary>
        /// <param name="supplierTransactionId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DelSupplierTransaction(long supplierTransactionId)
        {
            if (supplierTransactionId <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var result = _transactionService.DelSupplierTransaction(supplierTransactionId);
            if (result)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }
        /// <summary>
        /// 供应商对账退货报损报价根据id查询
        /// </summary>
        /// <param name="supplierTransactionId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSupplierTransaction(long supplierTransactionId)
        {
            if (supplierTransactionId <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var shareInfo = _transactionService.GetSupplierTransaction(supplierTransactionId);
            if (shareInfo != null)
            {
                return Ok(BaseResult.Result(shareInfo, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
        /// <summary>
        /// 供应商报损
        /// </summary>
        /// <param name="name"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSupplierTransactionLoss( DateTime? startDate, DateTime? endDate, int pageIndex = 1, int pageSize = 15)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var list = _transactionService.GetSupplierTransactionLoss( startDate, endDate, out var totalCount, pageIndex, pageSize);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
        /// <summary>
        /// 供应商对账报价分析
        /// </summary>
        /// <param name="name"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]

        public IActionResult GetSupplierTransactionQuotation( DateTime? startDate, DateTime? endDate, int pageIndex = 1, int pageSize = 15)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var list = _transactionService.GetSupplierTransactionQuotation( startDate, endDate, out var totalCount, pageIndex, pageSize);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
        /// <summary>
        /// 供应商对账
        /// </summary>
        /// <param name="name"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSupplierTransactionReconciliation( DateTime? startDate, DateTime? endDate, int pageIndex = 1, int pageSize = 15)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var list = _transactionService.GetSupplierTransactionReconciliation( startDate, endDate, out var totalCount, pageIndex, pageSize);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
        /// <summary>
        /// 供应商退货
        /// </summary>
        /// <param name="name"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSupplierTransactionReturn(DateTime? startDate, DateTime? endDate,  int pageIndex = 1, int pageSize = 15)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var list = _transactionService.GetSupplierTransactionReturn( startDate, endDate, out var totalCount, pageIndex, pageSize);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 供应商对账明细导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult SuppliersDetailDown()
        {
            var list = _transactionService.GetSuppliersDetailDown();
            if (list != null)
                return SuppliersDetailDown(list);
            else
                return Ok();
        }   
    }
}
